//
//  HWGameGiftBagController.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/30.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift
class HWGameGiftBagController: HWBaseViewController {
    var searchBgView:UIView!
    var searchLogoImage:UIImageView!
    var searchField:UITextField!
    
    var tableView:UITableView!
    var tableViewProtocol = HWGameGiftBagProtocol()
    let httpManager = HWHttpManager()
    var mutDataArr = NSMutableArray()
    var keyword = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        addAction()
        
        loadData()
        
        addRefresh()
        
    }
}

extension HWGameGiftBagController {
    private func initUI() -> Void {
        
        navigationView.titleLabel.text = "游戏礼包"
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        IQKeyboardManager.shared.enableAutoToolbar = true
        view.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        
        searchBgView = UIView()
        searchBgView.backgroundColor = .white
        searchBgView.layer.cornerRadius = 12*widthScale
        searchBgView.layer.borderWidth = 1*widthScale
        searchBgView.layer.borderColor = UIColor.colorWithHexString(hexString: "#CCCCCC").cgColor
        view.addSubview(searchBgView)
        
        searchLogoImage = UIImageView()
        searchLogoImage.image = UIImage(named: "search")
        searchBgView.addSubview(searchLogoImage)
        
        searchField = UITextField.init()
        searchField.textColor = UIColor.colorWithHexString(hexString: "#333333")
        searchField.font = UIFont.systemFont(ofSize: 14*widthScale)
        searchField.delegate = self
        searchField.returnKeyType = .search
        searchBgView.addSubview(searchField)
        
        let placeHoldLabel = UILabel.init()
        placeHoldLabel.text = "搜索游戏"
        placeHoldLabel.textColor = UIColor.colorWithHexString(hexString: "#AAAAAA")
        placeHoldLabel.font = UIFont.systemFont(ofSize: 14*widthScale)
        searchField.setValue(placeHoldLabel, forKey: "_placeholderLabel")
        searchField.addTarget(self, action: #selector(searchTextFieldChange(_:)), for: .editingChanged)
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
        
        view.bringSubviewToFront(navigationView)
    }
    
    private func initFrame() -> Void {
        
        searchBgView.snp.makeConstraints { make in
            make.top.equalTo(navigationView.snp.bottom).offset(10*widthScale)
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-20*widthScale)
            make.height.equalTo(36*widthScale)
        }
        
        searchLogoImage.snp.makeConstraints { make in
            make.left.equalTo(10*widthScale)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(16*widthScale)
        }
        
        searchField.snp.makeConstraints { make in
            make.left.equalTo(searchLogoImage.snp.right).offset(2*widthScale)
            make.centerY.equalToSuperview()
            make.right.equalTo(-10*widthScale)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(searchBgView.snp.bottom)
        }
    }
}

extension HWGameGiftBagController:UITextFieldDelegate {
    @objc func searchTextFieldChange(_ textField:UITextField) ->Void {
        if textField.text?.count == 0 {
//            self.searchTextClear!(true)
            keyword = ""
            loadData()
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//        self.searchText!(textField.text ?? "")
        textField.endEditing(true)
        keyword = textField.text ?? ""
        loadData()
        return true
    }
}

extension HWGameGiftBagController {
    func addAction() -> Void {
        tableViewProtocol.didClickRow = {
            mod in
            let gameGiftBagAlert = HWGameGiftBagAlert()
            gameGiftBagAlert.model = mod
            gameGiftBagAlert.show()
        }
    }
}

extension HWGameGiftBagController {
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    func loadData() -> Void {
        httpManager.getGameGiftBagInfo(keyword:keyword) { infoModel in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            if infoModel.count > 0 {
                self.mutDataArr.removeAllObjects()
                self.mutDataArr.addObjects(from: infoModel)
            }
            self.tableViewProtocol.models = self.mutDataArr as? [HWGameGiftBagModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            self.view.showHUD(title: error)
        }
    }
    @objc func loadMoreData() -> () {
        tableView.mj_footer?.endRefreshing()
        httpManager.getGameGiftBagInfoFooter(keyword:keyword) { models in
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.mutDataArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.mutDataArr as? [HWGameGiftBagModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
}
